package com.iyao_app.mvp.password;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.iyao_app.R;
import com.iyao_app.base.BaseFragment;
import com.iyao_app.entry.ResultModel;
import com.iyao_app.http.NetCallBack;
import com.iyao_app.http.NetUtils;
import com.iyao_app.util.Contants;
import com.iyao_app.util.CustomDialog;
import com.iyao_app.util.ResourcesUtils;
import com.iyaowoqu.base_library.constant.RegexConstants;
import com.iyaowoqu.base_library.util.KeyboardUtils;
import com.iyaowoqu.base_library.util.RegexUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/11/14.
 */

public class ForgetNextFragment extends BaseFragment {
    @BindView(R.id.et_login_account)
    EditText mEtLoginAccount;
    @BindView(R.id.et_message)
    EditText mEtMessage;
    @BindView(R.id.tv_fail)
    TextView mTvFail;
    Unbinder unbinder;
    private String phone;
    private String code;


    @Override
    public View initRootView(LayoutInflater inflater) {
        rootView = inflater.inflate(R.layout.fragment_forget_next, null);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void setViews() {
        phone = getArguments().getString(Contants.TYPE);
        code = getArguments().getString(Contants.OTHER_FIELD);
    }

    @Override
    public void initData() {
        mEtLoginAccount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (mTvFail.getVisibility() == View.VISIBLE) {
                    mTvFail.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mEtMessage.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (mTvFail.getVisibility() == View.VISIBLE) {
                    mTvFail.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mEtMessage.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    KeyboardUtils.hideSoftInput(mEtMessage);
                }
                return false;
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    String str = null;

    @OnClick(R.id.tv_next)
    public void onViewClicked() {
        if (TextUtils.isEmpty(mEtLoginAccount.getText().toString().trim())|| RegexUtils.isMatch(RegexConstants.ALL_ABC, mEtLoginAccount.getText().toString().trim())||RegexUtils.isMatch(RegexConstants.REGEX_INTEGER, mEtLoginAccount.getText().toString().trim())) {
            str = ResourcesUtils.getString(R.string.password_fail);
            Animation shake = AnimationUtils.loadAnimation(getActivity(), R.anim.shake);
            mTvFail.setVisibility(View.VISIBLE);
            mTvFail.startAnimation(shake);
            mTvFail.setText(str);
            return;
        }
        if (TextUtils.isEmpty(mEtMessage.getText().toString().trim()) || !mEtLoginAccount.getText().toString().trim().equals(mEtMessage.getText().toString().trim())) {
            str = ResourcesUtils.getString(R.string.password_again);
            Animation shake = AnimationUtils.loadAnimation(getActivity(), R.anim.shake);
            mTvFail.setVisibility(View.VISIBLE);
            mTvFail.startAnimation(shake);
            mTvFail.setText(str);
            return;
        }
        ((ForgetActivity) getActivity()).showDialog();
        NetUtils.getInstance().resetPassword(phone, code, mEtMessage.getText().toString().trim(), new NetCallBack() {
            @Override
            public void onSuccess(final String message, final int code, String response, ResultModel model) {
                ((ForgetActivity) getActivity()).closeDialog();
                if (code == Contants.SUCCESS_CODE) {
                    new CustomDialog(getActivity())
                            .setConfirmText(ResourcesUtils.getString(R.string.confirm))
                            .setContent(ResourcesUtils.getString(R.string.reset_password_success))
                            .setCanceled(false)
                            .setOnConfirmClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    finish();
                                }
                            })
                            .show();
                } else {
                    if (!TextUtils.isEmpty(message)) {
                        Animation shake = AnimationUtils.loadAnimation(getActivity(), R.anim.shake);
                        mTvFail.setVisibility(View.VISIBLE);
                        mTvFail.startAnimation(shake);
                        mTvFail.setText(message);
                    }
                }

            }

            @Override
            public void onFail(String message, int code) {
                ((ForgetActivity) getActivity()).closeDialog();
            }
        }, null);

    }
}
